package com.cqc.itemtouchhelper01;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.Collections;

/**
 * Created by ${cqc} on 2017/4/6.
 */

public class RecyclerViewCallBack extends ItemTouchHelper.Callback {

    //定义的接口
    private ItemTouchHelperCallback itemTouchHelperCallback;
    private MyAdapter adapter;

    public RecyclerViewCallBack(ItemTouchHelperCallback itemTouchHelperCallback) {
        this.itemTouchHelperCallback = itemTouchHelperCallback;
    }
//
//    public RecyclerViewCallBack(MyAdapter adapter) {
//        this.adapter = adapter;
//    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        itemTouchHelperCallback.onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        //滑动事件
//        Collections.swap(list,viewHolder.getAdapterPosition(),target.getAdapterPosition());
//        adapter.notifyDataSetChanged();
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        itemTouchHelperCallback.onItemDelete(viewHolder.getAdapterPosition());
    }


    public interface ItemTouchHelperCallback {
        void onItemDelete(int position);

        void onMove(int fromPosition, int toPosition);
    }
}
